<?php

class CptransactionsController extends Core_Controller_Action 
{
	private $_api;
	
	public function init() {	
		$this->_api = new Transactions();
	}
	public function indexAction(){
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$pagerLayout = $this->_api->getListWithPager( $page, $this->view->baseUrl() );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		
	}
	public function addAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$products = new Products();
		$this->view->products = $products->getListWithName();
		$prod_list = array();
		foreach ($this->view->products as $one)
		{
			$prod_list[$one['p_id']] = $one['title'];
		}
		
		$this->view->prod_list = $prod_list; 		
		$currency = new Currency();
		$cur_list = array();
		$this->view->cur_list = $currency->getList();
		foreach ($this->view->cur_list as $one)
		{
			$cur_list[$one['cur_id']] = $one['name'];
		}
		$this->view->cur_list = $cur_list;
		
		if($this->_request->isPost())
		{
			if($id)
				$tr = $this->_api->getItem( $id )->getFirst();
			else
				$tr = $this->_api;
			$tr->products_p_id = $this->_getParam('products_p_id');
			$tr->currency_cur_id = $this->_getParam('currency_cur_id');
			$tr->amount = $this->_getParam('amount');
			$tr->email = $this->_getParam('email');
			$tr->phone = $this->_getParam('phone');
			$tr->gg = $this->_getParam('gg');
			if($id)
			{
				if($tr->paid  == "no" AND $this->_getParam('paid') == "yes")
					$tr->paid_transaction();
				$tr->paid = $this->_getParam('paid');
				$tr->update_transaction();
			}
			else
			{
				$tr->add_transaction();
				if($this->_getParam('paid') == "yes")
					$tr->paid_transaction();
			}
			$this->_redirect('cptransactions');
		}
		if($id)
			$this->view->item = $this->_api->getItem( $id )->getFirst()->toArray();
	}
	public function paidAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$tr = $this->_api->getItem($id)->getFirst();
		$tr->paid_transaction();
		$this->_redirect('cptransactions');
	}
	public function sendAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$tr = $this->_api->getItem($id)->getFirst();
		$tr->send();
		$this->_redirect('cptransactions');
	}
	public function showAction(){
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$this->view->tran = $this->_api->getItemCur($id)->getFirst();
		
		$basket = new Basket();
		$pagerLayout = $basket->getListWithPager($id, $page);
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
	}
}
	